package com.ebelter.btcomlib.views.wave;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;

import com.ebelter.btcomlib.utils.log.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class WaveHelper {
    private static final String TAG = "WaveHelper";

    private static final long TOTAL_TIME = 5000;

    private WaveView mWaveView;

    private AnimatorSet mAnimatorSet;

    public WaveHelper(WaveView waveView) {
        mWaveView = waveView;
        mWaveView.setShowWave(true);
        initAnimation(0,0);
    }

    private long startTime;


    public void start() {

        if (mAnimatorSet != null) {
            mAnimatorSet.start();
            startTime=System.currentTimeMillis();
        }
    }
    ObjectAnimator waveShiftAnim;
    ObjectAnimator waterLevelAnim;
    ObjectAnimator amplitudeAnim;

    List<Animator> animators;

    private float progress;
    private float oldProgress;

    private void initAnimation(float start,float pro) {
        animators = new ArrayList<>();

        progress=pro;
        oldProgress=start;

        LogUtils.i(TAG,"---progress="+progress+"---oldProgress="+oldProgress);

        // horizontal animation.
        // wave waves infinitely.
        waveShiftAnim = ObjectAnimator.ofFloat(
                mWaveView, "waveShiftRatio", 0f, 1f);
        waveShiftAnim.setRepeatCount(ValueAnimator.INFINITE);
        waveShiftAnim.setDuration(1000);
        waveShiftAnim.setInterpolator(new LinearInterpolator());
        animators.add(waveShiftAnim);

        // vertical animation.
        // water level increases from 0 to center of WaveView
        waterLevelAnim = ObjectAnimator.ofFloat(
                mWaveView, "waterLevelRatio", start, pro);
        waterLevelAnim.setDuration(TOTAL_TIME);
        waterLevelAnim.setInterpolator(new DecelerateInterpolator());
        animators.add(waterLevelAnim);

        // amplitude animation.
        // wave grows big then grows small, repeatedly
        amplitudeAnim = ObjectAnimator.ofFloat(
                mWaveView, "amplitudeRatio", 0.000001f, 0.05f);
        amplitudeAnim.setRepeatCount(ValueAnimator.INFINITE);
        amplitudeAnim.setRepeatMode(ValueAnimator.REVERSE);
        amplitudeAnim.setDuration(5000);
        amplitudeAnim.setInterpolator(new LinearInterpolator());
        animators.add(amplitudeAnim);

        mAnimatorSet = new AnimatorSet();
        mAnimatorSet.playTogether(animators);
    }

    public void cancel() {
        if (mAnimatorSet != null) {
//            mAnimatorSet.cancel();
            mAnimatorSet.end();
        }
    }


    public void progress(float pro){


        mAnimatorSet.end();
        mAnimatorSet=null;

        mAnimatorSet = new AnimatorSet();
        long currentTime=System.currentTimeMillis();

        float start= (float) ((currentTime-startTime)*1.0/TOTAL_TIME);
        if (start>1){
            start=1;
        }

        LogUtils.i(TAG,"--------start1="+start);

      //  oldProgress=progress;//记录上一个进度

        start=(progress-oldProgress)*start+oldProgress;

        LogUtils.i(TAG,"---------------------start2="+start);

        initAnimation(start,pro);
        start();
    }
}
